package org.jeecg.modules.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.entity.FavVersion;
import org.jeecg.modules.mapper.FavVersionMapper;
import org.jeecg.modules.service.IFavVersionService;
import org.jeecg.modules.utils.FavCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * @Description: 版本控制
 * @Author: jeecg-boot
 * @Date: 2022-11-21
 * @Version: V1.0
 */
@Service
public class FavVersionServiceImpl extends ServiceImpl<FavVersionMapper, FavVersion> implements IFavVersionService {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * @return org.jeecg.common.api.vo.Result<java.lang.Object>
     * @author wei
     * @description 更新版本
     * @date 2022/11/21 19:26
     */
    @Override
    public boolean updateVersion(String type) {
        // 查询
        FavVersion favVersion = new FavVersion();
        favVersion.setType(type);
        QueryWrapper<FavVersion> queryWrapper = new QueryWrapper<>(favVersion);
        FavVersion version = this.getOne(queryWrapper);

        // 更新 / 添加
        String code = IdWorker.getIdStr();
        if (ObjectUtil.isEmpty(version)) {
            version = new FavVersion();
            version.setCode(code);
            version.setCreateTime(new Date());
            version.setType(type);
            this.save(version);
        } else {
            version.setCode(code);
            version.setType(type);
            version.setUpdateTime(new Date());
            this.updateById(version);
        }

        String key = FavCache.VERSION_CONTROL + "::" + type;
        redisUtil.del(key);
        return true;
    }
}
